package com.ruoyi.project.ins.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.ins.domain.InsMessage;
import com.ruoyi.project.ins.service.IInsMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/ins/message")
public class InsMessageController extends BaseController {
    @Autowired
    private IInsMessageService insMessageService;

    @PreAuthorize("@ss.hasPermi('ins:message:list')")
    @GetMapping("/list")
    public TableDataInfo list(InsMessage insMessage)
    {
        String username = getUsername();
        if (!username.equalsIgnoreCase("admin")) {
            insMessage.setCreateBy(username);
        }
        startPage();
        List<InsMessage> list = insMessageService.selectInsMessageList(insMessage);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('ins:message:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id)
    {
        return success(insMessageService.selectAccountById(id));
    }

    @PreAuthorize("@ss.hasPermi('ins:message:edit')")
    @Log(title = "ins消息详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody InsMessage insMessage)
    {
        insMessage.setUpdateBy(getUsername());
        return toAjax(insMessageService.updateInsMessage(insMessage));
    }

    @Log(title = "ins消息详情", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('ins:message:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, InsMessage insMessage)
    {
        String username = getUsername();
        if (!username.equalsIgnoreCase("admin")) {
            insMessage.setCreateBy(username);
        }
        List<InsMessage> list = insMessageService.selectInsMessageList(insMessage);
        ExcelUtil<InsMessage> util = new ExcelUtil<>(InsMessage.class);
        util.exportExcel(response, list, "消息数据");
    }
}
